var express = require('express');
var router = express.Router();

const db = require('./db/db');
const md5 = require('md5');

const { sendData } = require('./common/common')


/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('用户模块');
});

//注册接口
router.post('/register', function (req, res, next) {
  let { account, password } = req.body;

  if (account && password) {
    password = md5(password)
    let sql = ` insert into user (account,password) values('${account}','${password}')`;
    db.query(sql, (err, data) => {
      if (data.affectedRows) {
        sendData(res, 0, '注册成功');
      } else {
        sendData(res, 1, '注册失败,数据库操作失败');
      }
    })
  } else {
    sendData(res, 0, '注册失败，账号密码未传入');
  }


});
//登录接口
router.post('/login', function (req, res, next) {
  let { account, password } = req.body;

  if (account && password) {
    password = md5(password)
    let sql = `select * from user where account='${account}' and password='${password}'`;
    db.query(sql, (err, data) => {
      if (data.length) {
        sendData(res, 0, '登录成功', data[0]);
      } else {
        sendData(res, 1, '账号密码错误');
      }
    })
  } else {
    sendData(res, 1, '登录失败，账号或密码未传入');

  }


});










module.exports = router;
